package slim3.demo.controller.six;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;



public class TransAsiaTime {
	
    public static void main(String[] args) throws Exception {

//        TransAsiaTime test = new TransAsiaTime();
//        ArrayList<FlightBean> beans = test.updateKNH();
        
//        for (int i=0; i<beans.size(); i++) {
//            System.out.println(beans.get(i).toString());
//        }
        
        
//        String temp = "<td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\">B70887</td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\">1</td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\"><a href='#1' class=contentlink onclick=query('TSA','C','','');>TSA</a></td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\"><a href='#1' class=contentlink onclick=query('KNH','C','','');>KNH</a></td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\">12/08/21 11:15</td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\">12/08/21 12:10</td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\">12/08/21 11:15</td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\">12/08/21 12:32</td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\"><font color=\"Red\">12/08/21 11:25</font></td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\"><font color=\"Red\">12/08/21 12:31</font></td><td class=\"txtg\" align=\"center\" bgcolor=\"#B0DDFA\">抵達</td><td class=\"content\" align=\"center\" bgcolor=\"#B0DDFA\">-</td>";
//        System.out.println(temp.contains("B70887"));
//        System.out.println(temp.contains("B70889"));
//		
//		temp = "</tr><tr class=\"row1\">";
//		System.out.println(temp.contains("B70887"));
//        System.out.println(temp.contains("B70889"));
//        
//		temp = "<td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\">B70889</td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\">1</td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\"><a href='#1' class=contentlink onclick=query('TSA','C','','');>TSA</a></td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\"><a href='#1' class=contentlink onclick=query('KNH','C','','');>KNH</a></td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\">12/08/21 13:15</td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\">12/08/21 14:10</td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\">12/08/21 13:15</td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\">12/08/21 14:21</td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\"><font color=\"Red\">12/08/21 13:23</font></td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\"><font color=\"Red\">12/08/21 14:18</font></td><td class=\"txtg\" align=\"center\" bgcolor=\"#D1EEDC\">抵達</td><td class=\"content\" align=\"center\" bgcolor=\"#D1EEDC\">-</td>";
//		System.out.println(temp.contains("B70887"));
//        System.out.println(temp.contains("B70889"));
//        
//		temp = "</tr><tr class=\"row0\">";
//		System.out.println(temp.contains("B70887"));
//        System.out.println(temp.contains("B70889"));
        
//        for (int j=0; j<10; j++) {
//            
//            System.out.println(j);
//            
//            if (j == 5) {
//                
//                System.out.println("exit");
//                break;
//                
//            }
//            
//        }
        
        
    }
    
    
	
	/**
	 * 金門
	 */
	public ArrayList<FlightBean> updateKNH(ArrayList<FlightBean> beans) throws Exception{
//    public ArrayList<FlightBean> updateKNH() throws Exception{

        FlightBean bean = null;
        String temp = "";
        

        try {

          //time
            Date date = new Date();
            DateFormat secondFormat = new SimpleDateFormat("yyyy M d HH mm ss");
            TimeZone secondTime = TimeZone.getTimeZone("Asia/Taipei");
            secondFormat.setTimeZone(secondTime);
            
            String t = secondFormat.format(date);
            
            String [] ts = t.split(" ");
            
            String year = ts[0];
            String month = ts[1];
            String day = ts[2];
          
            

            //set today variable
            if (month.length()==1) month = "0" + month;
            if (day.length()==1) day = "0" + day;
            
                        
            
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tna.com.tw/en/b2c/service/bktt01.aspx?dep=TSA%20-%20Taipei%20(Songshan)%20TSA&arr=KNH%20-%20Kinmen%20KNH&day=" + year + "/" + month + "/" + day);
            
            
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
            
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

            
            //getting flight info
            ArrayList<String> flightNo = new ArrayList<String>();
            ArrayList<String> flightTime = new ArrayList<String>();
            
            
            
            String str;
            while ((str = in.readLine()) != null) {
                
                str = str.trim();
                bean = new FlightBean();
                
                if (str.contains("Flight No")) {
                    
                    //flight no
                    temp = str.substring(str.indexOf("GE")+2, str.indexOf("</span>"));
//                    System.out.println(temp);
                    flightNo.add(temp);
                    
                    
                    
                } else if (str.contains("Actual Time of Departure")) {

                    //estimated departure time
                    temp = str.substring(str.indexOf("Departure\">")+11, str.indexOf("</span>"));
                    if (temp.length()==5) {
//                        System.out.println(temp);
                        temp = temp.substring(0, 2) + temp.substring(3, 5);
                        flightTime.add(temp);
                    } else {
                        flightNo.remove(flightNo.size()-1);
                    }
                    
                }
            }
            
            in.close();
  
            
//            for (int i=0; i<flightNo.size(); i++) {
//                System.out.println(flightNo.get(i));
//                System.out.println(flightTime.get(i));
//            }
            
            //update beans with retrieved flight info
            for (int i=0; i<beans.size(); i++) {
                
                if (beans.get(i).getAirline().equals("GE")) {
                
                    for (int j=0; j<flightNo.size(); j++) {
                        
                        if (beans.get(i).getFlightNo().equals(flightNo.get(j))) {
                            
                            bean = beans.get(i);
                            bean.setActualTime(flightTime.get(j));
                            beans.set(i, bean);
                            break;
                            
                        }
                        
                    }
                    
                }
                
            }

            
                
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return beans;
            
    }
	  
    
    
	/**
     * 花蓮
     */
    public ArrayList<FlightBean> updateHUN(ArrayList<FlightBean> beans) throws Exception{
//    public ArrayList<FlightBean> updateKNH() throws Exception{

        FlightBean bean = null;
        String temp = "";
        

        try {

          //time
            Date date = new Date();
            DateFormat secondFormat = new SimpleDateFormat("yyyy M d HH mm ss");
            TimeZone secondTime = TimeZone.getTimeZone("Asia/Taipei");
            secondFormat.setTimeZone(secondTime);
            
            String t = secondFormat.format(date);
            
            String [] ts = t.split(" ");
            
            String year = ts[0];
            String month = ts[1];
            String day = ts[2];
          
            

            //set today variable
            if (month.length()==1) month = "0" + month;
            if (day.length()==1) day = "0" + day;
            
                        
            
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tna.com.tw/en/b2c/service/bktt01.aspx?dep=TSA%20-%20Taipei%20(Songshan)%20TSA&arr=HUN%20-%20Hualien%20HUN&day=" + year + "/" + month + "/" + day);
            
            
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
            
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

            
            //getting flight info
            ArrayList<String> flightNo = new ArrayList<String>();
            ArrayList<String> flightTime = new ArrayList<String>();
            
            
            
            String str;
            while ((str = in.readLine()) != null) {
                
                str = str.trim();
                bean = new FlightBean();
                
                if (str.contains("Flight No")) {
                    
                    //flight no
                    temp = str.substring(str.indexOf("GE")+2, str.indexOf("</span>"));
//                    System.out.println(temp);
                    flightNo.add(temp);
                    
                    
                    
                } else if (str.contains("Actual Time of Departure")) {

                    //estimated departure time
                    temp = str.substring(str.indexOf("Departure\">")+11, str.indexOf("</span>"));
                    if (temp.length()==5) {
//                        System.out.println(temp);
                        temp = temp.substring(0, 2) + temp.substring(3, 5);
                        flightTime.add(temp);
                    } else {
                        flightNo.remove(flightNo.size()-1);
                    }
                    
                }
            }
            
            in.close();
  
            
//            for (int i=0; i<flightNo.size(); i++) {
//                System.out.println(flightNo.get(i));
//                System.out.println(flightTime.get(i));
//            }
            
            //update beans with retrieved flight info
            for (int i=0; i<beans.size(); i++) {
                
                if (beans.get(i).getAirline().equals("GE")) {
                
                    for (int j=0; j<flightNo.size(); j++) {
                        
                        if (beans.get(i).getFlightNo().equals(flightNo.get(j))) {
                            
                            bean = beans.get(i);
                            bean.setActualTime(flightTime.get(j));
                            beans.set(i, bean);
                            break;
                            
                        }
                        
                    }
                    
                }
                
            }

            
                
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return beans;
            
    }
    
    
    
    /**
     * 澎湖馬公
     */
    public ArrayList<FlightBean> updateMZG(ArrayList<FlightBean> beans) throws Exception{
//    public ArrayList<FlightBean> updateKNH() throws Exception{

        FlightBean bean = null;
        String temp = "";
        

        try {

          //time
            Date date = new Date();
            DateFormat secondFormat = new SimpleDateFormat("yyyy M d HH mm ss");
            TimeZone secondTime = TimeZone.getTimeZone("Asia/Taipei");
            secondFormat.setTimeZone(secondTime);
            
            String t = secondFormat.format(date);
            
            String [] ts = t.split(" ");
            
            String year = ts[0];
            String month = ts[1];
            String day = ts[2];
          
            

            //set today variable
            if (month.length()==1) month = "0" + month;
            if (day.length()==1) day = "0" + day;
            
                        
            
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tna.com.tw/en/b2c/service/bktt01.aspx?dep=TSA%20-%20Taipei%20(Songshan)%20TSA&arr=MZG%20-%20Makung%20MZG&day=" + year + "/" + month + "/" + day);
            
            
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
            
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

            
            //getting flight info
            ArrayList<String> flightNo = new ArrayList<String>();
            ArrayList<String> flightTime = new ArrayList<String>();
            
            
            
            String str;
            while ((str = in.readLine()) != null) {
                
                str = str.trim();
                bean = new FlightBean();
                
                if (str.contains("Flight No")) {
                    
                    //flight no
                    temp = str.substring(str.indexOf("GE")+2, str.indexOf("</span>"));
//                    System.out.println(temp);
                    flightNo.add(temp);
                    
                    
                    
                } else if (str.contains("Actual Time of Departure")) {

                    //estimated departure time
                    temp = str.substring(str.indexOf("Departure\">")+11, str.indexOf("</span>"));
                    if (temp.length()==5) {
//                        System.out.println(temp);
                        temp = temp.substring(0, 2) + temp.substring(3, 5);
                        flightTime.add(temp);
                    } else {
                        flightNo.remove(flightNo.size()-1);
                    }
                    
                }
            }
            
            in.close();
  
            
//            for (int i=0; i<flightNo.size(); i++) {
//                System.out.println(flightNo.get(i));
//                System.out.println(flightTime.get(i));
//            }
            
            //update beans with retrieved flight info
            for (int i=0; i<beans.size(); i++) {
                
                if (beans.get(i).getAirline().equals("GE")) {
                
                    for (int j=0; j<flightNo.size(); j++) {
                        
                        if (beans.get(i).getFlightNo().equals(flightNo.get(j))) {
                            
                            bean = beans.get(i);
                            bean.setActualTime(flightTime.get(j));
                            beans.set(i, bean);
                            break;
                            
                        }
                        
                    }
                    
                }
                
            }

            
                
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return beans;
            
    }
	
	
    
}
